home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / CopyBits & CopyMask / CopyBits meets CopyMask.p < prev    next >
Text File  |  1995-04-04  |  4KB  |  151 lines

  1. program CopyBitsMeetsCopyMask;
  2.  
  3.     uses
  4. {$IFC UNDEFINED THINK_PASCAL}
  5.     Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  6.     OSUtils, ToolUtils, OSEvents,
  7. {$ENDC}
  8.         QDOffScreen;
  9.  
  10. (* Standard inits *)
  11.  
  12.     const
  13.         kLoopNumber = 100;
  14.  
  15.  
  16.     procedure InitToolbox;
  17.     begin
  18. {$IFC UNDEFINED THINK_PASCAL}
  19.         InitGraf(@qd.thePort);
  20.         InitFonts;
  21.         FlushEvents(everyEvent, 0);
  22.         InitWindows;
  23.         InitMenus;
  24.         TEInit;
  25.         InitDialogs(nil);
  26. {$ENDC}
  27.         InitCursor;
  28.     end; (*InitToolbox*)
  29.  
  30.     var
  31.         myWindow: WindowPtr;
  32.         windowRectangle: Rect;
  33.         h, v: Integer;
  34.         screenPort: GrafPtr;
  35.         screenDevice: GDHandle;
  36.         pictRectangle, srcRectangle: Rect;
  37.         coolPicture, maskPicture: PicHandle;
  38.         cTable: CTabHandle;
  39.         offscreenGWorld, maskGWorld: GrafPtr;
  40.         maskRegion: RgnHandle;
  41.  
  42.  
  43. { offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to}
  44. { refer to them by GrafPtrs, since the CopyBits calls get much cleaner.}
  45.  
  46. (* Variables for loops, timing and display *)
  47.         beforeTime, afterTime: LongInt;
  48.         loop: LongInt;
  49.         tempStr: Str255;
  50.  
  51.  
  52. begin
  53.     InitToolbox;
  54.  
  55. (* Load the picture*)
  56.     coolPicture := GetPicture(128);            (*PICT resource #128.*)
  57.     if (coolPicture = nil) then
  58.         begin
  59.             SysBeep(1);
  60.             ExitToShell;
  61.         end;
  62.  
  63.     maskPicture := GetPicture(129);            (*PICT resource #129.*)
  64.     if (maskPicture = nil) then
  65.         begin
  66.             SysBeep(1);
  67.             ExitToShell;
  68.         end;
  69.  
  70. (* Get its frame *)
  71.     pictRectangle := coolPicture^^.picFrame;
  72. (* Move it to 0,0 *)
  73.     OffsetRect(pictRectangle, -pictRectangle.left, -pictRectangle.top);
  74.  
  75. (*Set up the window*)
  76.     windowRectangle := pictRectangle;
  77.     OffsetRect(windowRectangle, 64, 64);
  78.     windowRectangle.bottom := windowRectangle.bottom + 100;
  79.     myWindow := NewCWindow(nil, windowRectangle, 'CopyBitsSpeedTest', true, 0, WindowPtr(-1), false, 0);
  80.     SetPort(myWindow);
  81.  
  82. {    DrawPicture(coolPicture, &pictRectangle);}
  83.  
  84. (* Remember it so we can get back after visiting a GWorld *)
  85.     GetGWorld(GWorldPtr(screenPort), screenDevice);
  86.  
  87. (* Create a GWorld to draw the PICT in *)
  88.     if (noErr <> NewGWorld(GWorldPtr(offscreenGWorld), 0, pictRectangle, nil, nil, [])) then
  89.         ExitToShell;
  90. (*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*)
  91.     if LockPixels(CGrafPtr(offscreenGWorld)^.portPixMap) then ;
  92.     SetGWorld(GWorldPtr(offscreenGWorld), nil);
  93.  
  94.     PaintRect(pictRectangle);
  95.     DrawPicture(coolPicture, pictRectangle);
  96.  
  97. (* Create a GWorld to draw the mask in *)
  98.     if (noErr <> NewGWorld(GWorldPtr(maskGWorld), 1, pictRectangle, nil, nil, [])) then
  99.         ExitToShell;
  100. (*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*)
  101.     if (LockPixels(CGrafPtr(maskGWorld)^.portPixMap)) then ;
  102.     SetGWorld(GWorldPtr(maskGWorld), nil);
  103.  
  104.     EraseRect(pictRectangle);
  105.     DrawPicture(maskPicture, pictRectangle);
  106.  
  107.     maskRegion := NewRgn;
  108.     if noErr <> BitMapToRegion(maskRegion, maskGWorld^.portBits) then
  109.         begin
  110.         end; {I ignore errors here}
  111.  
  112. (* GWorlds done. Set back port and device! *)
  113.  
  114.     SetGWorld(GWorldPtr(screenPort), screenDevice);
  115.     CopyBits(offscreenGWorld^.portBits, myWindow^.portBits, pictRectangle, pictRectangle, srcCopy, nil);
  116.  
  117. { CopyBits}
  118.  
  119.     srcRectangle := pictRectangle;
  120.  
  121.     beforeTime := TickCount;
  122.     for loop := 0 to kLoopNumber - 1 do
  123.         begin
  124.             srcRectangle := pictRectangle;
  125.             OffsetRect(srcRectangle, 0, loop mod 20 - 10);
  126.             CopyBits(offscreenGWorld^.portBits, myWindow^.portBits, srcRectangle, pictRectangle, srcCopy, maskRegion);
  127.         end;
  128.     afterTime := TickCount;
  129.     MoveTo(10, windowRectangle.bottom - 80 - 64);
  130.     NumToString(afterTime - beforeTime, tempStr);
  131.     DrawString('CopyBits: ');
  132.     DrawString(tempStr);
  133.     DrawString(' ticks.');
  134.  
  135.     beforeTime := TickCount;
  136.     for loop := 0 to kLoopNumber - 1 do
  137.         begin
  138.             srcRectangle := pictRectangle;
  139.             OffsetRect(srcRectangle, 0, loop mod 20 - 10);
  140.             CopyMask(offscreenGWorld^.portBits, maskGWorld^.portBits, myWindow^.portBits, srcRectangle, pictRectangle, pictRectangle);
  141.         end;
  142.     afterTime := TickCount;
  143.     MoveTo(10, windowRectangle.bottom - 50 - 64);
  144.     NumToString(afterTime - beforeTime, tempStr);
  145.     DrawString('CopyMask: ');
  146.     DrawString(tempStr);
  147.     DrawString(' ticks.');
  148.  
  149.     while not Button do
  150.         ;
  151. end. (*main*)